define(['zepto', 'butterfly/view','../../bsl',
    'text!./orientation.html'
], function($, View, bsl, orientation) {
    var me = null;
    var id = null;
    
    var view = View.extend({
        events: {
            "tap #getOrient":"getOrient",
            "tap #watchOrient":"watchOrient",
            "tap #watchStop":"watchStop",
            "tap #back":"back",
            "tap #openDoc":"openDoc",
            "tap #clicked1":"clicked1",
            "tap #clicked2":"clicked2"
        },
        render: function() {
            me = this;
        },

        onShow: function() {

            // 绑定自定义事件 滑动切换日历等           


        },
        getOrient:function() {
            console.info( "获取设备当前方向信息" );
            bsl.orientation.getCurrentOrientation( function ( o ) {
                console.info( "alpha：" + o.alpha + "\nbeta：" + o.beta + "\ngamma：" + o.gamma );
            }, function ( e ) {
                console.info( "获取失败:" + e.message );
            } );
        },
        
        watchOrient:function() {
            if ( id ) {
                return;
            }
            console.info( "监听设备方向变化信息" );
            id = bsl.orientation.watchOrientation( function ( o ) {
                console.info( "监听设备方向变化信息\n" + "alpha：" + o.alpha + "\nbeta：" + o.beta + "\ngamma：" + o.gamma );
            }, function ( e ) {
                bsl.orientation.clearWatch( id );
                id = null;
                console.info( "监听失败:" + e.message );
            } );
        },
        watchStop:function() {
            if ( id ) {
                console.info( "停止监听设备方向变化信息" );
                bsl.orientation.clearWatch( id );
                id = null;
            } else {
                console.info( "没有监听设备方向变化" );
            }
        },
        clicked1:function(){
            window.butterfly.navigate('orientation_level.html', {
                trigger: true
            });
        },

        clicked2:function(){
            window.butterfly.navigate('orientation_compass.html', {
                trigger: true
            });
        },

        back : function(e){
            window.history.go(-1);
        },
        openDoc:function(){
            window.butterfly.navigate('com.no5.cordovademo/doc/orientation', {
                trigger: true
            });
        }

            });
    return view;
});
